package com.devops.admin.graphql;

import com.devops.admin.model.DOSCluster;
import com.devops.admin.model.DOSClusterNamespace;
import com.devops.admin.model.TableResult;
import com.devops.admin.po.DOSClusterNamespacePo;
import com.devops.admin.po.DOSClusterPo;
import com.devops.admin.po.DOSEnvClusterPo;
import com.devops.admin.service.*;
import com.devops.admin.vo.DOSClusterNamespaceVo;
import com.devops.admin.vo.DOSClusterVo;
import com.devops.admin.vo.DOSPageVo;
import com.devops.common.annotation.DOSLog;
import com.devops.constant.DevOpsConstant;
import com.netflix.graphql.dgs.DgsComponent;
import com.netflix.graphql.dgs.DgsMutation;
import com.netflix.graphql.dgs.DgsQuery;
import com.netflix.graphql.dgs.InputArgument;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @ClassName DOSClusterDataFetcher
 * @Description TODO
 * @Author heyabo
 * @Date 2022/8/3 15:47
 * @Version 1.0
 **/
@DgsComponent
public class DOSClusterDataFetcher {

    @Autowired
    DOSClusterService dosClusterService;
    @Autowired
    DOSClusterNamespaceService dosClusterNamespaceService;
    @Autowired
    DOSClusterInfoService dosClusterInfoService;
    @Autowired
    DOSEnvClusterRelationService dosEnvClusterRelationService;
    @Autowired
    DOSProjectEnvClusterNamespaceRelationService dosProjectEnvClusterNamespaceRelationService;

    @DgsMutation
    @DOSLog(description = "保存集群")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR},logical = Logical.OR)
    public DOSClusterPo saveCLuster(@InputArgument DOSClusterVo cluster) {

        TableResult<DOSClusterPo> list = dosClusterService.saveCluster(cluster);
        return list.getList().get(0);
    }

    /**
     * @Author heyabo
     * @Description 更新集群信息
     * @Date 09:59 2022/3/18
     * @Param [cluster]
     * @return java.lang.Boolean
     **/
    @DgsMutation
    @DOSLog(description = "更新集群信息")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR},logical = Logical.OR)
    public Boolean updateCluster(@InputArgument DOSClusterVo cluster){

        return dosClusterService.updateCluster(cluster);
    }

    /**
     * @Author heyabo
     * @Description 删除集群
     * @Date 15:24 2022/3/18
     * @Param [clusterId]
     * @return java.lang.Boolean
     **/
    @DgsMutation
    @DOSLog(description = "删除集群")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR},logical = Logical.OR)
    public Boolean deleteCluster(@InputArgument Integer clusterId){
        return dosClusterService.deleteById(clusterId);
    }

    /**
     * @Author heyabo
     * @Description 添加集群的命名空间
     * @Date 14:51 2022/3/18
     * @Param []
     * @return java.lang.Boolean
     **/
    @DgsMutation
    @DOSLog(description = "添加集群的命名空间")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR},logical = Logical.OR)
    public DOSClusterNamespace addClusterNamespace(@InputArgument DOSClusterNamespace namespace){

        return dosClusterNamespaceService.saveNamespace(namespace);
    }

    /**
     * @Author heyabo
     * @Description 更新集群命名空间
     * @Date 16:00 2022/3/18
     * @Param [namespace]
     * @return java.lang.Boolean
     **/
    @DgsMutation
    @DOSLog(description = "更新集群命名空间")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR},logical = Logical.OR)
    public Boolean updateClusterNamespace(@InputArgument DOSClusterNamespace namespace){

        return dosClusterNamespaceService.updateByNamespaceId(namespace);
    }

    @DgsQuery
    @DOSLog(description = "根据id查询集群信息")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR},logical = Logical.OR)
    public DOSCluster getClusterById(@InputArgument Integer clusterId) {
        return dosClusterService.getById(clusterId);
    }

    @DgsQuery
    @DOSLog(description = "分页查询集群列表")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR},logical = Logical.OR)
    public TableResult<DOSClusterPo> getClusters(@InputArgument DOSCluster cluster,@InputArgument DOSPageVo pageVo) {
        return dosClusterService.getList(cluster,pageVo);
    }

    /**
     * @Author heyabo
     * @Description 根据集群id获取命名空间
     * @Date 11:46 2022/3/18
     * @Param [clusterId]
     * @return java.util.List<com.devops.admin.model.DOSClusterNamespace>
     **/
    @DgsQuery
    @DOSLog(description = "根据集群id获取命名空间")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR},logical = Logical.OR)
    public TableResult<DOSClusterNamespace> getClusterNamespaces(@InputArgument DOSClusterNamespace namespace, @InputArgument DOSPageVo pageVo){
        return dosClusterNamespaceService.getByClusterId(namespace,pageVo);
    }

    /**
     * @Author heyabo
     * @Description 根据命名空间名称获取命名空间
     * @Date 16:35 2022/3/18
     * @Param [namespace]
     * @return com.devops.admin.model.DOSClusterNamespace
     **/
    @DgsQuery
    @DOSLog(description = "根据命名空间名称获取命名空间")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR},logical = Logical.OR)
    public TableResult<DOSClusterNamespace> queryClusterNamespaceByName(@InputArgument DOSClusterNamespaceVo namespace, @InputArgument DOSPageVo pageVo){
        return dosClusterNamespaceService.getNamespaceByName(namespace,pageVo);
    }

    /**
     * @Author heyabo
     * @Description  查询所有的集群
     * @Date 14:48 2022/6/23
     * @Param [cluster, pageVo]
     * @return java.util.List<com.devops.admin.po.DOSClusterPo>
     **/
    @DgsQuery
    @DOSLog(description = "查询所有的命名空间")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR},logical = Logical.OR)
    public List<DOSClusterPo> getAllClusters(@InputArgument DOSCluster cluster) {
        return dosClusterService.getAllList(cluster);
    }

    /**
     * @Author yuiop
     * @Description 获取环境、集群和命名空间的级连
     * @Date 10:56 2022/7/6
     * @Param []
     * @return java.util.List<com.devops.admin.po.DOSEnvClusterPo>
     **/
    @DgsQuery
    @RequiresPermissions(DevOpsConstant.RoleCode.LOGIN_USER)
    public List<DOSEnvClusterPo> getEnvClusterNamespaceCascade(){
        return dosClusterService.getCascade();
    }

    /**
     * 根据projectId和envType获取集群和命名空间的级联
     * @Author yuiop
     * @param projectId
     * @param envType
     * @return
     */
    @DgsQuery
    @RequiresPermissions(DevOpsConstant.RoleCode.LOGIN_USER)
    public List<DOSClusterNamespacePo> getClustersByProjectIdAndEnvType(@InputArgument Integer projectId, @InputArgument Integer envType) {
        return dosProjectEnvClusterNamespaceRelationService.getClustersByProjectIdAndEnvType(projectId,envType);
    }

    /**
     * @Author heyabo
     * @Description 根据登录人员的身份获取集群和命名空间的级联
     * @Date 09:00 2022/7/22
     * @Param []
     * @return java.util.List<com.devops.admin.po.DOSClusterNamespacePo>
     **/
    @DgsQuery
    @RequiresPermissions(DevOpsConstant.RoleCode.LOGIN_USER)
    public List<DOSClusterNamespacePo> getClusterNamespace(){

        return dosClusterService.queryClusterNamespace();
    }

    /**
     * @Author heyabo
     * @Description 根据集群id获取命名空间
     * @Date 10:02 2022/8/23
     * @Param [namespace, pageVo]
     * @return com.devops.admin.model.TableResult<com.devops.admin.model.DOSClusterNamespace>
     **/
    @DgsQuery
    @DOSLog(description = "根据集群id获取命名空间")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR},logical = Logical.OR)
    public List<DOSClusterNamespace> getNamespaces(@InputArgument Integer clusterId){
        return dosClusterNamespaceService.getNamespaces(clusterId);
    }
}
